import _ from 'lodash';
import { useReducer } from 'react';
export default function usePrevious() {
    //usePrevious自定义hooks无法实现数据跨组件共享用useReducer
    const reducer = (state: any[], action: { type: string, data: any }) => {
        let returnState = _.cloneDeep(state);
        switch (action.type) {
            case 'push':
                {
                    returnState = [...returnState, action.data];
                    return returnState;
                }
            case 'delete':
                {
                    return returnState.reduce((pre: any, item: any, idx: number) => {
                        if (idx !== action.data) {
                            pre.push(item);
                        }
                        return pre;
                    }, []);
                }
            default:
                return returnState;
        }
    }
    const [state, dispatch] = useReducer(reducer, []);//reducer函数处理器,
    return [state, dispatch];
};